package org.jeecg.modules.charge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.ZoeException;
import org.jeecg.modules.charge.entity.ComCardInfo;
import org.jeecg.modules.charge.mapper.ComCardInfoMapper;
import org.jeecg.modules.charge.service.IComCardInfoService;
import org.jeecg.modules.patient.entity.PatBasicInfo;
import org.jeecg.modules.patient.service.IPatBasicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 就诊卡信息
 * @Author: jeecg-boot
 * @Date:   2022-05-14
 * @Version: V1.0
 */
@Service
public class ComCardInfoServiceImpl extends ServiceImpl<ComCardInfoMapper, ComCardInfo> implements IComCardInfoService {

    private static final String CARD="card";
    private static final String PATIENT="patient";

    @Lazy
    @Autowired
    private IPatBasicInfoService patBasicInfoService;

    @Override
    public void activate(ComCardInfo cardInfo) {
        if (StringUtils.isEmpty(cardInfo.getCardNumber())){
            throw new ZoeException("卡号不可为空");
        }
        QueryWrapper<ComCardInfo> queryWrapper = new QueryWrapper<ComCardInfo>()
                .eq("card_number", cardInfo.getCardNumber());
        if (StringUtils.isNotEmpty(cardInfo.getCardType())){
            queryWrapper.eq("card_type",cardInfo.getCardType());
        }
        ComCardInfo oldData = baseMapper.selectOne(queryWrapper);
        if (oldData!=null){
            throw new ZoeException("已被激活");
        }else {
            save(cardInfo);
        }
    }

    @Override
    public Map<String, Object> getDataByCardNumber(String cardNumber,String keywords) {
        HashMap<String, Object> dataMap = new HashMap<>(5);
        ComCardInfo cardInfo = baseMapper.getByCardNumber(cardNumber);
        if (cardInfo==null){
//            throw new ZoeException("无数据");
            return null;
        }
        dataMap.put(CARD,cardInfo);

        if (StringUtils.isNotEmpty(keywords)){
            if (keywords.contains(PATIENT)){
                PatBasicInfo patBasicInfo = patBasicInfoService.getById(cardInfo.getPatientId());
                dataMap.put(PATIENT,patBasicInfo);
            }
        }

        return dataMap;
    }

    @Override
    public ComCardInfo getByPatient(String patientId) {
        return null;
    }
}
